import ComponentsPlugin from 'unplugin-vue-components/webpack'
import NutUIResolver from '@nutui/auto-import-resolver'

const path = require('path')
const config = {
  projectName: 'culture_mini_app',
  date: '2023-4-10',
  designWidth(input) {
    // 配置 NutUI 375 尺寸
    if (input?.file?.replace(/\\+/g, '/').indexOf('@nutui') > -1) {
      return 375
    }
    // 全局使用 Taro 默认的 750 尺寸
    return 750
  },
  deviceRatio: {
    640: 2.34 / 2,
    750: 1,
    828: 1.81 / 2,
    375: 2 / 1,
  },
  sourceRoot: 'src',
  outputRoot: `dist/${process.env.TARO_ENV}`,
  plugins: ['taro-plugin-pinia', '@tarojs/plugin-html'],
  defineConstants: {},
  copy: {
    patterns: [],
    options: {},
  },
  sass: {
    resource: [path.resolve(__dirname, '..', 'src/assets/styles/variable.scss')],
  },
  alias: {
    '@/apis': path.resolve(__dirname, '..', 'src/apis/index'),
    '@/components': path.resolve(__dirname, '..', 'src/components'),
    '@/assets': path.resolve(__dirname, '..', 'src/assets'),
    '@/store': path.resolve(__dirname, '..', 'src/store'),
    '@/utils': path.resolve(__dirname, '..', 'src/utils'),
    '@/questions': path.resolve(__dirname, '..', 'src/subPackages/question/assets'),
    '@/introduce': path.resolve(__dirname, '..', 'src/subPackages/introduce/assets'),
    '@/ranking': path.resolve(__dirname, '..', 'src/subPackages/ranking/assets'),
  },
  pxtransform: {
    config: {
      selectorBlackList: ['nut-'],
    },
  },
  framework: 'vue3',
  compiler: 'webpack5',
  cache: {
    enable: false, // Webpack 持久化缓存配置，建议开启。默认配置请参考：https://docs.taro.zone/docs/config-detail#cache
  },
  mini: {
    webpackChain(chain) {
      // 解决 ES Module 兼容性问题
      chain.resolve.extensions.add('.mjs')

      chain.plugin('unplugin-vue-components').use(
        ComponentsPlugin({
          resolvers: [NutUIResolver({ taro: true })],
        })
      )
    },
    // 开启JS和CSS压缩
    terser: {
      compress: {
        drop_console: true, // 去除console.log
      },
      mangle: true,
    },
    cssnano: {
      preset: ['default', { discardComments: { removeAll: true } }], // 去除所有注释
    },
    postcss: {
      pxtransform: {
        enable: true,
        config: {},
      },
      url: {
        enable: true,
        config: {
          limit: 1024, // 设定转换尺寸上限
        },
      },
      cssModules: {
        enable: false, // 默认为 false，如需使用 css modules 功能，则设为 true
        config: {
          namingPattern: 'module', // 转换模式，取值为 global/module
          generateScopedName: '[name]__[local]___[hash:base64:5]',
        },
      },
    },
  },
}

module.exports = function (merge) {
  if (process.env.NODE_ENV === 'development') {
    return merge({}, config, require('./dev'))
  }
  return merge({}, config, require('./prod'))
}
